<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class pages extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->model('m_pages');

	}//end Login

	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : $_GET['task'];
		switch($task){
			case "list":
				$this->pages_list();
			break;
			case "create":
				$this->pages_create();
			break;
			case "update":
				$this->pages_update();
			break;
			case "delete":
				$this->pages_delete();
			break;
			case "search":
				$this->pages_search();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}

	function index(){
		redirect(base_url());
	}

	function read(){
		$urlori=$this->uri->uri_string();
		$url=$this->uri->segment(3);
		$url=str_replace('_',' ',$url);

		if(preg_match('/html/i',$url)){
			$url=str_replace('.html','',$url);
			$data["parentid"]=$this->m_public_function->get_menuparent_by_pages($url);
			if($data["parentid"]==""){
				$data["parentid"]=$this->m_public_function->get_menuparent_by_url($urlori);
			}
			if($data["parentid"]==""){
				$data["parentid"]=$_SESSION['parent_id'];
			}else{
				$_SESSION['parent_id']=$data["parentid"];
			}

			$data["content"]=$this->m_pages->pages_view($url);
		}

		$this->load->view('v_pages',$data);
	}

	function pages_load(){
		$this->load->view('admin/v_pages_list');
	}

	function pages_addedit(){
		$data = array();
		$id = $this->input->get_post('id');
		if($id != false){
			$data['dataDetail'] = $this->getDetail($id);
		}
		$this->load->view('admin/v_pages_post', $data);
	}

	function pages_list(){
		$query = $this->input->post('query');
		$pagenum = $_GET['pagenum'];
		$pagesize = $_GET['pagesize'];
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;
		$result=$this->m_pages->pages_list($query, $start, $end);
		echo $result;
	}

	function pages_save(){
		$id = $this->input->post('id');
		$kategori = $this->input->post('kategori');
		$judul = $this->input->post('judul');
		$tanggal = $this->input->post('tanggal');
		$tag = $this->input->post('tag');
		$isi = $this->input->post('isi');
		$publish = $this->input->post('publish');
		$gambar = $this->input->post('gambar');
		$task = $this->input->post('task');
		$params = array(
			'id'=>$id,
			'data'=>array(
				'judul'=>$judul,
				'tag'=>$tag,
				'isi'=>$isi,
				'publish'=>$publish,
				'image_page'=>$gambar
			),
			'task'=>$task
		);
		$result = $this->m_pages->pages_save($params);
		echo $result;
	}

	function getDetail($id){
		$this->db->where('id', $id);
		$query = $this->db->get('page');

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function pages_delete(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : $id;
		$result = $this->m_pages->pages_delete($id);
		echo $result;
	}

	function view(){

		$permalink=$_GET["p"];
		$data["content"]=$this->m_public_function->get_page($permalink);
		$data["menus"]=$this->m_public_function->get_menu_home();
		$this->load->view('v_pages', $data);
	}

	function pages_search(){

	}

	function pages_update(){

	}

	function pages_create(){

	}

}
?>
